<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data="rolesList" border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row
              :class="['bdbottom',i1==0?'bdtop':'']"
              v-for="(item1,i1) in props.row.children"
              :key="item1.id"
            >
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="closeRolesTag(props.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二级权限 -->
              <el-col :span="19">
                <el-row
                  :class="[i2==0?'':'bdtop']"
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="closeRolesTag(props.row,item2.id)"
                      type="success"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 渲染三级权限 -->
                  <el-col :span="18">
                    <el-tag
                      @close="closeRolesTag(props.row,item3.id)"
                      closable
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button
              size="mini"
              @click="showSetRolesDialog(scope.row)"
              type="warning"
              icon="el-icon-setting"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分配权限的弹出框 -->
      <el-dialog
        title="分配权限"
        :visible.sync="setRolesDialogVisible"
        width="50%"
        :before-close="setRolesDialogClosed"
      >
        <el-tree
          :data="rolesDialogData"
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree"
          highlight-current
          :props="treeProps"
          :default-checked-keys="treeArray"
        ></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRolesDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subRolesDialo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>


<script>
//
import { truncate } from "fs";
export default {
  data() {
    return {
      rolesList: [],
      setRolesDialogVisible: false,
      rolesDialogData: null,
      treeProps: {
        children: "children",
        label: "authName"
      },
      treeArray: [],
      roleId: null
    };
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get("roles");
      if (res.meta.status !== 200) return this.$message(res.meta.msg);
      this.rolesList = res.data;
    },
    async closeRolesTag(use, id) {
      const result = await this.$confirm(
        "此操作将永久删除该权限, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (result !== "confirm") return this.$message.info("取消了删除");
      const { data: res } = await this.$http.delete(
        "roles/" + use.id + "/rights/" + id
      );
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      use.children = res.data;
    },
    async showSetRolesDialog(row) {
      this.roleId = row.id;
      this.getTreeArray(row, this.treeArray);
      this.setRolesDialogVisible = true;
      // console.log(row);
      const { data: res } = await this.$http.get("rights/tree");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      // console.log(res);
      this.rolesDialogData = res.data;
    },
    getTreeArray(node, arr) {
      if (!node.children) return arr.push(node.id);
      node.children.forEach(element => {
        this.getTreeArray(element, arr);
      });
    },
    setRolesDialogClosed() {
      this.setRolesDialogVisible = false;
      this.treeArray = [];
    },
    async subRolesDialo() {
      const arr = this.$refs.tree
        .getHalfCheckedKeys()
        .concat(this.$refs.tree.getCheckedKeys());
      const { data: res } = await this.$http.post(
        "roles/" + this.roleId + "/rights",
        { rids: arr.join(",") }
      );
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.getRolesList();
      this.setRolesDialogClosed();
    }
  },
  created() {
    this.getRolesList();
  }
};
</script>

<style lang="less" scoped>
.el-row {
  display: flex;
  align-items: center;
}
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
</style>